package cn.tedu.tmallpassport;

import cn.hutool.core.bean.BeanUtil;
import cn.tedu.tmallpassport.pojo.entity.User;
import cn.tedu.tmallpassport.pojo.param.UserLoginInfoParam;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisTest2 {
    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;

    // 执行Key的操作时，直接通过redisTemplate的API即可
    // 读写Redis中的string数据时，需要先通过redisTemplate.opsForValue()获取ValueOperations对象
    // 读写Redis中的list数据时，需要先通过redisTemplate.opsForList()获取ListOperations对象
    // 读写Redis中的set数据时，需要先通过redisTemplate.opsForSet()获取SetOperations对象
    // 读写Redis中的hash数据时，需要先通过redisTemplate.opsForHash()获取HashOperations对象

//-----------string数据操作
    @Test
    void stringSet(){
        //set key value
        // 存入数据
        ValueOperations<String, Serializable> operations=redisTemplate.opsForValue();
        operations.set("username1","cxk");
    }

    @Test
    void stringGet(){
        // get key
        // 取出数据
        ValueOperations<String, Serializable> operations=redisTemplate.opsForValue();
        Serializable serializable = operations.get("username1");
        System.out.println(serializable);
    }

    @Test
    void stringObjectSet(){
        // set key value
        UserLoginInfoParam loginInfoParam=new UserLoginInfoParam();
        loginInfoParam.setUsername("root");
        loginInfoParam.setPassword("123456");

        ValueOperations<String, Serializable> operations=redisTemplate.opsForValue();
        operations.set("loginInfoParam",loginInfoParam);
    }

    @Test
    void stringObjectGet(){
        ValueOperations<String, Serializable> operations=redisTemplate.opsForValue();
        Serializable serializable = operations.get("loginInfoParam");
        System.out.println(serializable);
    }


    // -------------------key数据的操作------------------------

    @Test
    void keys(){
        //key pattern
        //查询所有的key
        Set<String> keys = redisTemplate.keys("*");
        System.out.println(keys);
    }

    @Test
    void delete(){
        //del key
        // 删除key
        Boolean username1 = redisTemplate.delete("username1");
        System.out.println(username1);
    }

    @Test
    void deleteBatch(){
        // 删除key集合
        Set<String> keys=new HashSet<>();
        keys.add("n1");
        keys.add("n2");
        keys.add("n3");
        keys.add("n4");

        Long delete = redisTemplate.delete(keys);
        System.out.println(delete);
    }

    @Test
    void expire(){
        // 设置时间
        // expire key seconds
         String key="n2";
        Boolean expire = redisTemplate.expire(key, 1, TimeUnit.MINUTES);
        System.out.println(expire);
    }

    @Test
    void getExpire(){
        // 获取时间
        // ttl key
        String key="n2";
        Long expire = redisTemplate.getExpire(key);
        System.out.println(expire);
    }

    //--------list数据操作-------------
    @Test
    void listRightPush(){
        // 使用`rpush`可以从右侧压栈
        // repush key element
        List<User> userList=new ArrayList<>();
        for (int i = 0; i <= 8; i++) {
            User user=new User();
            user.setUsername("user-"+i);
            userList.add(user);

        }

        ListOperations<String, Serializable> operations=redisTemplate.opsForList();
        for (User user: userList){
            operations.rightPush("user-list",user);
        }
    }
    @Test
    void listSize(){
        // llen key
        // 获取list类型数据的长度
        ListOperations<String, Serializable> operations=redisTemplate.opsForList();
        Long size = operations.size("user-list");
        System.out.println(size);
    }

    @Test
     void listRange(){
        // lrange key start stop
        // 截取list的片段
        ListOperations<String, Serializable> operations=redisTemplate.opsForList();
        List range = operations.range("user-list", 0, -1);
        System.out.println(range);

        List<User> userList=range;
        for (User user : userList){
            System.out.println(user.getUsername());
        }
    }

    //------------set数据的操作

    @Test
    void setAdd(){
        // 存入数据
        SetOperations<String, Serializable> operations=redisTemplate.opsForSet();
        operations.add("srting-set","s1");
        operations.add("srting-set","s2","s3","s4");
    }

    @Test
    void setSize(){
        // 获取成员的数量
        SetOperations<String, Serializable> operations=redisTemplate.opsForSet();
        Long size = operations.size("string-set");
        System.out.println(size);
    }

    @Test
    void setMembers(){
        // 查看全部成员
        // smembers key
        SetOperations<String, Serializable> operations=redisTemplate.opsForSet();
        Set members = operations.members("string-set");
        Set<String> stringSet=members;
        for (String s : stringSet){
            System.out.println(s);
        }
    }

    // --------------------hash数据的操作------------
    @Test
    void hashSet(){
        // 存入Value的某个字段的值
        // hset key field value
        HashOperations<String, Object ,Object> operations=redisTemplate.opsForHash();
        operations.put("hash1","id","996");
    }

    @Test
    void hashGet(){
        // 获取hash类型数据中某个字段对应的值
        HashOperations<String,Object,Object> operations=redisTemplate.opsForHash();
        Object o = operations.get("hash1", "id");
        System.out.println(o);
    }

    @Test
    void hashPutAll(){
        // 存入Value的多个字段与值
        UserLoginInfoParam loginInfoParam=new UserLoginInfoParam();
        loginInfoParam.setPassword("123456");
        loginInfoParam.setUsername("root");

        Map<String, Object> map= BeanUtil.beanToMap(loginInfoParam);
        HashOperations<String,Object,Object> operations=redisTemplate.opsForHash();
        operations.putAll("hash2",map);
    }

    @Test
    void hashEntries(){
        HashOperations<String,Object,Object> operations=redisTemplate.opsForHash();
        Map<Object, Object> hash2 = operations.entries("hash2");
        System.out.println(hash2);

        if (hash2.size()>0){
            UserLoginInfoParam loginInfoParam=BeanUtil.mapToBean(hash2,UserLoginInfoParam.class,true,null);
            System.out.println(loginInfoParam);
        }
    }
}

